package com.wuyan.locationdog.ui.device


import androidx.activity.viewModels
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bean.DeviceBeanContent
import com.arcsoft.commonbase.ext.defaultText
import com.arcsoft.commonbase.ext.radius
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.databinding.InspectDetailActivityBinding
import com.wuyan.locationdog.help.gpsSignalText
import com.wuyan.locationdog.viewmodel.DeviceViewModel


class DeviceInspectDetailActivity : BaseBindingActivity<InspectDetailActivityBinding>() {

    private val mViewModel: DeviceViewModel by viewModels()

    override fun initData() {
        val deviceBeanContent = intent.getSerializableExtra("DeviceBeanContent") as DeviceBeanContent

        BarUtils.transparentStatusBar(this)

        setTitleText("检测详情")

        binding.apply {
            imei.text = deviceBeanContent.imei
        }
        val map = HashMap<String, Any>()
        map["imei"] = deviceBeanContent.imei
        mViewModel.deviceCheck(map)

        binding.apply {
            accOval.radius(3F)
            networkOval.radius(3F)
            powerOval.radius(3F)
            gpsSignalOval.radius(3F)
        }
    }

    override fun observe() {
        super.observe()
        mViewModel.deviceCheckBean.observe(this) {
            binding.apply {
                accOval.radius(3F)
                networkOval.radius(3F)
                powerOval.radius(3F)
                gpsSignalOval.radius(3F)

                if (it.content.acc == 0) {
                    acc.text = "关机"
                    acc.setTextColor(ColorUtils.getColor(R.color.red_oval))

                    accOval.setBackgroundColor(ColorUtils.getColor(R.color.red_oval))
                } else {
                    acc.text = "开机"
                    acc.setTextColor(ColorUtils.getColor(R.color.green_oval))

                    accOval.setBackgroundColor(ColorUtils.getColor(R.color.green_oval))
                }
                deviceModelDesc.text = it.content.deviceModelDesc.defaultText()
                gpsNum.text = it.content.gpsNum.defaultText()
                iccid.text = it.content.iccid.defaultText()

                locSetting.text = it.content.locSetting.defaultText()
                communicationTimeDesc.text = it.content.communicationTimeDesc.defaultText()

                if (it.content.network.isNullOrEmpty()) {
                    network.text = "未连接"
                    network.setTextColor(ColorUtils.getColor(R.color.red_oval))
                    networkOval.setBackgroundColor(ColorUtils.getColor(R.color.red_oval))
                } else {
                    network.text = it.content.network.toString()
                    network.setTextColor(ColorUtils.getColor(R.color.green_oval))
                    networkOval.setBackgroundColor(ColorUtils.getColor(R.color.green_oval))
                }
                if (it.content.power < 20) {
                    power.text = "电量不足"
                    power.setTextColor(ColorUtils.getColor(R.color.yellow_oval))
                    powerOval.setBackgroundColor(ColorUtils.getColor(R.color.yellow_oval))
                } else {
                    power.text = "电量充足"
                    power.setTextColor(ColorUtils.getColor(R.color.green_oval))

                    powerOval.setBackgroundColor(ColorUtils.getColor(R.color.green_oval))
                }
                gpsSignal.text = gpsSignalText(it.content.gpsSignal.toInt()).defaultText()

                gpsSignal.setTextColor(ColorUtils.getColor(R.color.green_oval))

                gpsSignalOval.setBackgroundColor(ColorUtils.getColor(R.color.green_oval))

                remotePower.text = it.content.power.toString() + "%"


            }
        }
    }

}